/***
 * Author: Andrey Zhmaylik
 * 		   Erez Bar
 * 
 * What:  Displays live pictures from server.
 * How:	  Requests a snapshot from server and displays it. 
 */

package house.keepper;  
  
import java.io.IOException;
import java.io.OptionalDataException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
  
public class Live extends Activity  {            
    private Button m_liveBtn; 
    private ImageView m_iv;
  
    /**  
     * Called when the activity is first created.  
     */  
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
		this.setTitle("Click refresh to show live picture");
		setContentView(R.layout.live_pics);
		m_liveBtn = (Button)findViewById(R.id.Live_viewBtn);
		m_iv = (ImageView)findViewById(R.id.image);
		//Show current picture.
		ShowLivePic();
    
		m_liveBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	ShowLivePic();	    
            }
		});
		
    }

	private void ShowLivePic() 
	{
		byte[] file_buf = null;
		Bitmap bm;
		try
		{
			try {
				Connection.s_out.writeInt(ServerCommands.ViewLivePics.value());
				if (Connection.s_in.readInt() == ServerCommands.FileIsOk
						.value())
					file_buf = (byte[]) Connection.s_in.readObject();
				else
					return;
			} catch (OptionalDataException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		bm = BitmapFactory.decodeByteArray(file_buf, 0, file_buf.length);
		m_iv.setImageBitmap(bm);
	}	
	
}  
